package com.xiyun.cms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiyun.cms.entity.CmsBanner;
import com.xiyun.cms.service.CmsBannerService;
import com.xiyun.common.response.result.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 首页banner表 前端控制器  后台
 * </p>
 *
 * @author xiyun
 * @since 2020-12-25
 */
@CrossOrigin
@RestController
@RequestMapping("/xiyun/cms/banner/after")
public class AfterBannerController {

    @Autowired
    private CmsBannerService bannerService;

    @ApiOperation("分页查询banner")
    @GetMapping("pageBanner/{page}/{size}")
    public R pageBanner(@PathVariable long page,
                        @PathVariable long size){
        Page<CmsBanner> bannerPage = new Page<>(page,size);
        bannerService.page(bannerPage,null);
        return R.ok().data("items",bannerPage.getRecords()).data("total",bannerPage.getTotal());
    }

    @ApiOperation("添加banner")
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CmsBanner cmsBanner){
        bannerService.save(cmsBanner);
        return R.ok();
    }

    @ApiOperation("删除banner")
    @DeleteMapping("delBanner/{id}")
    public R delBanner(@PathVariable String id){
        bannerService.removeById(id);
        return R.ok();
    }

    @ApiOperation("修改banner")
    @PostMapping("updateBanner")
    public R updateBanner(@RequestBody CmsBanner cmsBanner){
        bannerService.updateById(cmsBanner);
        return R.ok();
    }

    @ApiOperation("根据id查询banner")
    @GetMapping("getBannerById/{id}")
    public R getBannerById(@PathVariable String id){
        CmsBanner banner = bannerService.getById(id);
        return R.ok().data("banner",banner);
    }
}
